import { getSetting } from "@/api/setting";
import { titleController } from "@/Utils";

export default {
    namespaced: true, //开启命名空间
    state: {
        loading: false,
        data: null
    },
    mutations: {
        setLoading(state, payload) {
            state.loading = payload
        },
        setData(state, payload) {
            state.data = payload
        }
    },

    actions: {
        async fetchSetting(ctx) {
            //把loading 的加载动画效果设置为ture
            ctx.commit("setLoading", true);
            //ajax 获取服务端数据
            const resp = await getSetting();
            //调用setData 把获取到的服务器数据 存到仓库里面
            ctx.commit("setData", resp);
            //在给仓库存完数据的时候 表示我们的数据获取已经结束了 所以我们把loading 设置为false
            ctx.commit("setLoading", false);
            if (resp.favicon) {
                // <link rel="shortcut icon " type="images/x-icon" href="./favicon.ico">
                let link = document.querySelector("link[ref='shortcut icon']");
                if (link) {
                    return;
                }
                link = document.createElement("link");
                link.rel = "shortcut icon";
                link.type = "images/x-icon";
                link.href = resp.favicon;
                document.querySelector("head").appendChild(link);
            }
            if (resp.siteTitle) {
                titleController.setSiteTitle(resp.siteTitle);
            }
        }
    }
}
